package it.openHWHackaton.arduinoTableSoccer;

import it.openHWHackaton.Match.Match;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

public class UITimer extends Actor{

	private BitmapFont font;
	private int elapsedTime=0;
	private boolean isStart=false;
	private Timer timer;
	private ShapeRenderer shapeRenderer;
	int w=Gdx.graphics.getWidth();
	int h=Gdx.graphics.getHeight();
	private Match match=null;
	public UITimer(Match match){
		this.match=match;

		timer = new Timer();  //At this line a new Thread will be created
        timer.schedule(new RemindTask(), 0,1000); //delay in milliseconds
		
		 font = new BitmapFont(Gdx.files.internal("data/Calibri-2.fnt"),
		         Gdx.files.internal("data/Calibri_0-2.png"), false);
		font.setColor(Color.WHITE);
	}
	@Override
	public void draw(SpriteBatch batch, float alpha){
	
	      
  		 String minute =String.valueOf(TimeUnit.SECONDS.toMinutes(elapsedTime) - (TimeUnit.SECONDS.toHours(elapsedTime)* 60));
		 String second =String.valueOf(TimeUnit.SECONDS.toSeconds(elapsedTime) - (TimeUnit.SECONDS.toMinutes(elapsedTime) *60));
		if(minute.length()==1)
			minute="0"+minute;
		if(second.length()==1)
			second="0"+second;
		
		 font.draw(batch, match.getTeam1().getNomeTeam()+" - "+match.getTeam2().getNomeTeam()+"  "+minute+":"+second, 0,h);

	}
	
	public void start(){
		isStart=true;
	}
	public void pause(){
		isStart=false;
	}
	public void reset(){
		elapsedTime=0;
	}
	class RemindTask extends TimerTask {
		
        @Override
        public void run() {
        	if(isStart)
        		elapsedTime+=1;      
        }
    }
}
